//
//  TestViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/25.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "TestViewController.h"
#import "PurseView.h"
#import "ResponseManager.h"
#import "ResponseManager+WalletInfo.h"
#import "PurseCustomTableViewCell.h"


static NSString *testIdentifier = @"textTableViewCell";
@interface TestViewController ()<PurseViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)PurseView *headerView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableDictionary *dataSource;
@property (nonatomic, strong)NSMutableArray *dataArr;

@end

@implementation TestViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"钱包";
        [self setUpView];
    [self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated {
    [ResponseManager getWalletInfoCompletionHandler:^(FBResponse *response) {
        if (response.success) {
            NSLog(@"钱包 = %@",response.resultValue);
            self.dataSource = response.resultValue;
            self.dataArr = self.dataSource[@"pay"];
            [self.tableView reloadData];
        }else {
            NSLog(@"%@",response.resultDesc);
        }
    }];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
}





- (void)setUpView {

    self.headerView = [PurseView loadPurseView];
    _headerView.ownerVC = self;
    _headerView.delegate = self;
    _headerView.frame = CGRectMake(0, 0, kScreenWidth, 300 * kScreenHeightScale);
    [self.view addSubview:self.headerView];

}

- (void)purseView:(PurseView *)purseView didTapedCategoryButtonAtIndex:(NSInteger)index {
    
    if (index == 0) {
        _headerView.leftView.hidden = NO;
        _headerView.rightView.hidden = YES;
        NSLog(@"%@",self.dataSource[@"pay"]);
        
        self.dataArr = self.dataSource[@"income"];
        [self.tableView reloadData];

        
    }
    if (index == 1) {
        _headerView.rightView.hidden = NO;
        _headerView.leftView.hidden = YES;
        self.dataArr = self.dataSource[@"pay"];
        [self.tableView reloadData];

    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PurseCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:testIdentifier];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.time.text = self.dataArr[indexPath.row][@"add_time"];
    cell.niceName.text = self.dataArr[indexPath.row][@"to_user_nicename"];
    cell.bonus.text = [NSString stringWithFormat:@"%@元",self.dataArr[indexPath.row][@"bag_price"]];
    
    return cell;
    
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height, kScreenWidth, kScreenHeight - self.headerView.frame.size.height) style:UITableViewStylePlain];
        UINib *nib = [UINib nibWithNibName:@"PurseCustomTableViewCell" bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:testIdentifier];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    
    return _tableView;
}

- (NSMutableDictionary *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableDictionary dictionary];
    
    }
    return  _dataSource;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray  array];
    }
    return _dataArr;
}
@end
